package com.aaa.mq;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

@Configuration
public class ActivityDeadMqConfig {

    // 主题模式交换机：交换机，路由key
    public static final String ACTIVITY_DEAD_EX = "activity-dead_ex";// 死信交换机
    public static final String ACTIVITY_DEAD_START_QUEUE = "activity-dead-start-queue";// 活动队列:添加秒杀活动之后，保存到队列中
    public static final String ACTIVITY_DEAD_END_QUEUE = "activity-dead-end-queue";// 活动队列:添加秒杀活动之后，保存到队列中

    // 死信交换机
    @Bean(value = "activityDeadEx")
    public TopicExchange activityDeadEx() {
        return new TopicExchange(ACTIVITY_DEAD_EX, true, false);
    }


    // 活动开始队列
    @Bean("activityDeadStartQueue")
    public Queue activityStartQueue() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("x-max-length", 20);
        map.put("x-overflow", "drop-head");
        return new Queue(ACTIVITY_DEAD_START_QUEUE, true, true, false, map);
    }

    // 绑定
    @Bean(value = "bindActivityDeadStartQueue")
    public Binding bindActivityDeadStartQueue(@Qualifier("activityDeadEx") TopicExchange exchange, @Qualifier("activityDeadStartQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("dead.start");
    }

    // 活动开始队列
    @Bean("activityDeadEndQueue")
    public Queue activityDeadEndQueue() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("x-max-length", 20);
        map.put("x-overflow", "drop-head");
        return new Queue(ACTIVITY_DEAD_END_QUEUE, true, true, false, map);
    }

    // 绑定
    @Bean(value = "bindActivityDeadEndQueue")
    public Binding bindActivityDeadEndQueue(@Qualifier("activityDeadEx") TopicExchange exchange, @Qualifier("activityDeadEndQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("dead.end");
    }


}
